package day8;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public
class TestCallable implements Callable<Integer> {
    String str;

    public
    TestCallable(String str) {
        this.str = str;
    }
    @Override
    public
    Integer call() throws Exception {
        Integer sum = 0;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == 'A') {
                sum++;
                Thread.sleep(100);
            }
        }
        return sum;
    }

    public static
    void main(String[] args) throws ExecutionException, InterruptedException {
        TestCallable t1 = new TestCallable("AABBBB");
        //Callable不能直接启动，需要借助FutureTask,FutureTask实现了RunnableFuture接口,属于Runnable类型
        FutureTask<Integer> ft=new FutureTask(t1);
        Thread t2=new Thread(ft);
        t2.start();
        //ft.get()可以获取线程执行后的结果   但是会阻塞 需要等待call方法执行完成才会有返回值

        Integer result = ft.get();
        System.out.println("A出现了"+result+"次");

    }
}